<template>
    <div>
      <!-- 搜索结果页-头部导航 -->
      <div class="search-result-container">
        <!-- 点击实现后退效果 -->
        <van-nav-bar
          title="搜索结果"
          left-arrow
          @click-left="$router.go(-1)"
          fixed
        />
      </div>
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        :immediate-check="false"
        offset="50"
      >
      <ArticleItem v-for="(item ,index) in searchResultList"
      :key="index"
      :artObj="item"
      :isShow= 'false'
      @click.native="itemClickFn(item.art_id)"
      ></ArticleItem>
      </van-list>
    </div>
</template>

<script>
import { timeAgo } from '@/utils/date'
import ArticleItem from '../components/Articleitem.vue'
import { searchResultAPI } from '@/api'
export default {
  name: 'SearchResult',
  data () {
    return {
      searchResultList: [],
      page: 1,
      finished: false,
      loading: false // 加载状态
    }
  },
  methods: {
    async onLoad () {
      if (this.searchResultList.length > 0) {
        this.page++
        const res = await searchResultAPI({
          page: this.page,
          q: this.$route.params.kw
        })

        if (res.data.data.results.length === 0) {
          this.finished = true
          return
        }
        console.log(res)
        this.searchResultList = [...this.searchResultList, ...res.data.data.results]
        this.loading = false
      }
    },
    itemClickFn (id) {
      this.$router.push({
        path: `/ArticleDetail?art_id=${id}`
      }

      )
    }
  },
  async created () {
    // console.log(this.$router)
    const res = await searchResultAPI({
      page: this.page,
      q: this.$route.params.kw
    })
    console.log(res)
    const { data: { data: { results } } } = res
    results.forEach(obj => {
      obj.pubdate = timeAgo(obj.pubdate)
    })
    this.searchResultList = results
  },
  components: {
    ArticleItem
  }
}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>
